<template>
	<view>
		<!-- component/login-view.wxml -->
		<view class="contain" @tap="handleTap" v-if="isLogin"><slot></slot></view>
		<view class="contain" v-else>
			<view class="auth" @tap="getPhoneNumber"></view>
			<slot></slot>
		</view>
	</view>
</template>

<script>
const app = getApp();

const $passport = require('../../request/api/v1/passport');

const $token = require('../../utils/token.js');

const $invite = require('../../utils/invite.js');

const $flow = require('../../utils/flow.js');

import { openId } from '../../utils/cache.js';
export default {
	data() {
		return {
			isLogin: false,
			show: false,
			loginInfo: {
				token: '',
				uid: 0,
				apply_info: 0
			},
			payload2: {}
		};
	},
	/**
	 * 组件的属性列表
	 */
	props: {
		/**
		 * 外部传入需要进入下一步的数据，原样返回
		 */
		payload: {
			type: Object,
			default: () => {
				return {};
			}
		},
		getOpenId: {
			type: Boolean,
			default: false
		},

		/**
		 * 登录状态入口
		 */
		hasLogin: {
			type: Boolean,
			default: false
		}
	},
	watch: {
		hasLogin: {
			handler: function(val) {
				this.isLogin = val;
			},

			immediate: true,
			deep: true
		}
	},
	mounted: function() {
		let isLogin = $token.get() != '';

		if (isLogin) {
			// this.setData({
			//   isLogin: true,
			// });
		} else {
			// if (this.getOpenId) {
			//     this.getOpenIdFun();
			// }
		}
	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		cancel() {
			this.show = false;
		},

		setLoginInfo() {
			console.log('this.', this.loginInfo);
			this.loginInfo.token;
			$token.set(this.loginInfo.token);
			$invite.setMyUserId(this.loginInfo.uid);
			$flow.setFlow(this.loginInfo.apply_info);
			this.isLogin = true;
		},

		getOpenIdFun() {
			const that = this;
			uni.login({
				success(res) {
					if (res.code) {
						$passport
							.getOpenId(
								{
									code: res.code
								},
								true
							)
							.then(res => {
								openId.set(res.openid); // wx.setStorageSync("openId", res.openid);

								that.$emit('toAction', {
									detail: {
										my_center_banner: res.my_center_banner
									}
								});
							});
					}
				}
			});
		},

		bindGetUserInfo(e) {
			uni.getUserProfile({
				desc: '用于完善会员资料',
				success: res => {
					let info = res.userInfo;
					var that = this;
					$passport
						.setUserInfo({
							face: info.avatarUrl,
							nickname: info.nickName,
							token: $token.get()
						})
						.then(() => {
							that.show = false;
							app.globalData.is_info = 1; // this.setLoginInfo();
						});
				}
			});
		},

		getPhoneNumber(e) {
			uni.navigateTo({
				url: '/pages/loginType/codeLogin'
			});
			return;
			var that = this;

			if (e.currentTarget.dataset) {
				this.payload2 = e.currentTarget.dataset;
			}

			if (!e.detail.encryptedData) {
				return false;
			}

			$passport
				.weixinLogin({
					openid: openId.get(),
					scene: uni.getStorageSync('WXAPP_SCENE'),
					invite_id: $invite.get_Invite_Id(),
					// apply_id: $invite.getInviteCode(),
					encrypted_data: e.detail.encryptedData,
					iv: e.detail.iv
				})
				.then(res => {
					this.loginInfo.token = res.token;
					this.loginInfo.uid = res.uid;
					this.loginInfo.apply_info = res.apply_info;
					this.loginInfo = this.loginInfo;

					if (res.is_info == 1) {
						this.setLoginInfo();
						this.$emit('onLoginSuccess', {
							detail: {
								payload: Object.assign(that.payload, that.payload2, res)
							}
						});
					} else {
						this.show = true;
					}
				})
				.catch(() => {});
		},

		//已登录状态
		handleTap: function() {
			this.$emit('onLoginSuccess', {
				detail: {
					payload: this.payload
				}
			});
		}
	}
};
</script>
<style>
@import './login-view.css';
</style>
